package no.ntnu.fp.handlers;

import java.io.IOException;

import no.ntnu.fp.connection.ReceiveWorker;
import no.ntnu.fp.handler.Handler;
import no.ntnu.fp.handlers.exceptions.NetworkException;
import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.impl.MeetingRoomsFetchedMessage;
import no.ntnu.fp.net.co.Connection;
import no.ntnu.fp.services.MeetingRoomService;
import no.ntnu.fp.utils.MeetingRoomSchedule;

public class GetMeetingRoomsHandler implements Handler {
	private final MeetingRoomService meetingRoomService;
	
	public GetMeetingRoomsHandler(MeetingRoomService meetingRoomService) {
		this.meetingRoomService = meetingRoomService;
	}

	@Override
	public void handleMessage(Message message) {
		MeetingRoomSchedule schedule = meetingRoomService.getMeetingRoomSchedule();
		
		MeetingRoomsFetchedMessage reply = new MeetingRoomsFetchedMessage(schedule);
		
		Connection connection = ReceiveWorker.getThreadLocalConnection();
		try {
			connection.send(reply.toString());
		} catch (IOException e) {
			throw new NetworkException("Unable to send reply", e);
		}
	}
}
